<!DOCTYPE HTML>
<html lang="en" >
    <!-- Start book Django课程讲义 -->
    <head>
        <!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>JWT的使用 | Django课程讲义</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        <meta name="author" content="BigCat">
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../../data/statistics/index.html" />
    
    
    <link rel="prev" href="../../data/logins/jwt.html" />
    

        <!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
        
    <div class="book"
        data-level="1.3"
        data-chapter-title="JWT的使用"
        data-filepath="data/logins/DRF-JWT.md"
        data-basepath="../.."
        data-revision="Sat Apr 20 2019 23:53:09 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        美多管理后台
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="data/logins/index.html">
            
                
                    <a href="../../data/logins/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        管理员登录
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="data/logins/cors.html">
            
                
                    <a href="../../data/logins/cors.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        CORS跨域
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="data/logins/jwt.html">
            
                
                    <a href="../../data/logins/jwt.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        JWT的原理和构成
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="1.3" data-path="data/logins/DRF-JWT.html">
            
                
                    <a href="../../data/logins/DRF-JWT.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        JWT的使用
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="data/statistics/index.html">
            
                
                    <a href="../../data/statistics/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        数据统计
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="data/statistics/total.html">
            
                
                    <a href="../../data/statistics/total.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        用户总数统计
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="data/statistics/increase.html">
            
                
                    <a href="../../data/statistics/increase.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        日增用户统计
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="data/statistics/active.html">
            
                
                    <a href="../../data/statistics/active.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        日活跃用户统计
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="data/statistics/orders.html">
            
                
                    <a href="../../data/statistics/orders.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        日下单用户统计
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="data/statistics/month.html">
            
                
                    <a href="../../data/statistics/month.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        月增用户统计
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="data/statistics/goods.html">
            
                
                    <a href="../../data/statistics/goods.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.</b>
                        
                        日分类商品访问量
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="data/users/index.html">
            
                
                    <a href="../../data/users/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        用户管理
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="data/users/search.html">
            
                
                    <a href="../../data/users/search.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        获取查询用户
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="data/users/adduser.html">
            
                
                    <a href="../../data/users/adduser.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        增加用户
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="data/goods/index.html">
            
                
                    <a href="../../data/goods/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        商品管理
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="data/goods/sku.html">
            
                
                    <a href="../../data/goods/sku.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        SKU表管理
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1.1" data-path="data/goods/getsku.html">
            
                
                    <a href="../../data/goods/getsku.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.1.</b>
                        
                        获取SKU表数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.1.2" data-path="data/goods/createsku.html">
            
                
                    <a href="../../data/goods/createsku.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.2.</b>
                        
                        保存SKU表数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.1.3" data-path="data/goods/updatesku.html">
            
                
                    <a href="../../data/goods/updatesku.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.3.</b>
                        
                        修改SKU表数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.1.4" data-path="data/goods/deletesku.html">
            
                
                    <a href="../../data/goods/deletesku.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.4.</b>
                        
                        删除SKU表数据
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="data/spu/spu.html">
            
                
                    <a href="../../data/spu/spu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        SPU表管理
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.2.1" data-path="data/spu/getspu.html">
            
                
                    <a href="../../data/spu/getspu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.1.</b>
                        
                        获取SPU表数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2.2" data-path="data/spu/createspu.html">
            
                
                    <a href="../../data/spu/createspu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.2.</b>
                        
                        保存SPU表数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2.3" data-path="data/spu/updatespu.html">
            
                
                    <a href="../../data/spu/updatespu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.3.</b>
                        
                        修改SPU表数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2.4" data-path="data/spu/deletespu.html">
            
                
                    <a href="../../data/spu/deletespu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.4.</b>
                        
                        删除SPU表数据
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="data/specs/specs.html">
            
                
                    <a href="../../data/specs/specs.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        规格表管理
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.3.1" data-path="data/specs/getspecs.html">
            
                
                    <a href="../../data/specs/getspecs.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.1.</b>
                        
                        获取规格表数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3.2" data-path="data/specs/createspecs.html">
            
                
                    <a href="../../data/specs/createspecs.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.2.</b>
                        
                        保存规格表数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3.3" data-path="data/specs/updatespecs.html">
            
                
                    <a href="../../data/specs/updatespecs.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.3.</b>
                        
                        修改规格表数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3.4" data-path="data/specs/deletespecs.html">
            
                
                    <a href="../../data/specs/deletespecs.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.4.</b>
                        
                        删除SPU表数据
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="data/options/option.html">
            
                
                    <a href="../../data/options/option.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        规格选项表管理
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.4.1" data-path="data/options/getoptionmd.html">
            
                
                    <a href="../../data/options/getoptionmd.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.1.</b>
                        
                        获取规格表数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4.2" data-path="data/options/createoption.html">
            
                
                    <a href="../../data/options/createoption.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.2.</b>
                        
                        保存规格表数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4.3" data-path="data/options/updateoption.html">
            
                
                    <a href="../../data/options/updateoption.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.3.</b>
                        
                        修改规格表数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4.4" data-path="data/options/deleteoption.html">
            
                
                    <a href="../../data/options/deleteoption.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.4.</b>
                        
                        删除SPU表数据
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="data/images/image.html">
            
                
                    <a href="../../data/images/image.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        图片表管理
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.5.1" data-path="data/images/getimage.html">
            
                
                    <a href="../../data/images/getimage.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.1.</b>
                        
                        获取图片表数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.5.2" data-path="data/images/createimage.html">
            
                
                    <a href="../../data/images/createimage.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.2.</b>
                        
                        保存图片表数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.5.3" data-path="data/images/updateimage.html">
            
                
                    <a href="../../data/images/updateimage.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.3.</b>
                        
                        修改图片表数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.5.4" data-path="data/images/deleteimage.html">
            
                
                    <a href="../../data/images/deleteimage.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.4.</b>
                        
                        删除图片表数据
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="data/orders/index.html">
            
                
                    <a href="../../data/orders/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        订单管理
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="data/orders/getorders.html">
            
                
                    <a href="../../data/orders/getorders.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        获取订单表列表数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="data/orders/getdetailorder.html">
            
                
                    <a href="../../data/orders/getdetailorder.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        获取订单表详情数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="data/orders/updatestatus.html">
            
                
                    <a href="../../data/orders/updatestatus.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        修改订单表状态
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="data/system.html">
            
                
                    <a href="../../data/system.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        系统管理
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="data/permission/index.html">
            
                
                    <a href="../../data/permission/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        权限管理
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1.1" data-path="data/permission/getpermission.html">
            
                
                    <a href="../../data/permission/getpermission.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.1.</b>
                        
                        获取权限数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.1.2" data-path="data/permission/createpermission.html">
            
                
                    <a href="../../data/permission/createpermission.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.2.</b>
                        
                        保存权限数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.1.3" data-path="data/permission/updatepermission.html">
            
                
                    <a href="../../data/permission/updatepermission.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.3.</b>
                        
                        更新权限数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.1.4" data-path="data/permission/deletepermission.html">
            
                
                    <a href="../../data/permission/deletepermission.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.4.</b>
                        
                        删除权限数据
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="data/group/index.html">
            
                
                    <a href="../../data/group/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        用户组管理
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.2.1" data-path="data/group/getgruop.html">
            
                
                    <a href="../../data/group/getgruop.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.1.</b>
                        
                        获取用户组数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2.2" data-path="data/group/creategroup.html">
            
                
                    <a href="../../data/group/creategroup.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.2.</b>
                        
                        保存用户组数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2.3" data-path="data/group/updategroup.html">
            
                
                    <a href="../../data/group/updategroup.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.3.</b>
                        
                        修改用户组数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2.4" data-path="data/group/deletegroup.html">
            
                
                    <a href="../../data/group/deletegroup.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.4.</b>
                        
                        删除用户组数据
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="data/admin/index.html">
            
                
                    <a href="../../data/admin/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        管理员管理
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.3.1" data-path="data/admin/getadmin.html">
            
                
                    <a href="../../data/admin/getadmin.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.1.</b>
                        
                        获取管理员
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3.2" data-path="data/admin/createadmin.html">
            
                
                    <a href="../../data/admin/createadmin.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.2.</b>
                        
                        保存管理员
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3.3" data-path="data/admin/updateadmin.html">
            
                
                    <a href="../../data/admin/updateadmin.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.3.</b>
                        
                        更新管理员
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3.4" data-path="data/admin/deleteadmin.html">
            
                
                    <a href="../../data/admin/deleteadmin.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.4.</b>
                        
                        删除管理员
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../../" >Django课程讲义</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="django-rest-framework-jwt">Django REST framework JWT</h1>
<p><strong>&#x6211;&#x4EEC;&#x5728;&#x9A8C;&#x8BC1;&#x5B8C;&#x7528;&#x6237;&#x7684;&#x8EAB;&#x4EFD;&#x540E;&#xFF08;&#x68C0;&#x9A8C;&#x7528;&#x6237;&#x540D;&#x548C;&#x5BC6;&#x7801;&#xFF09;&#xFF0C;&#x9700;&#x8981;&#x5411;&#x7528;&#x6237;&#x7B7E;&#x53D1;JWT&#xFF0C;&#x5728;&#x9700;&#x8981;&#x7528;&#x5230;&#x7528;&#x6237;&#x8EAB;&#x4EFD;&#x4FE1;&#x606F;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x8FD8;&#x9700;&#x6838;&#x9A8C;&#x7528;&#x6237;&#x7684;JWT&#x3002;</strong></p>
<p><strong>&#x5173;&#x4E8E;&#x7B7E;&#x53D1;&#x548C;&#x6838;&#x9A8C;JWT&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;Django REST framework JWT&#x6269;&#x5C55;&#x6765;&#x5B8C;&#x6210;&#x3002;</strong></p>
<p><a href="http://getblimp.github.io/django-rest-framework-jwt/" target="_blank">&#x6587;&#x6863;&#x7F51;&#x7AD9;http://getblimp.github.io/django-rest-framework-jwt/</a></p>
<h2 id="&#x5B89;&#x88C5;&#x914D;&#x7F6E;">&#x5B89;&#x88C5;&#x914D;&#x7F6E;</h2>
<p>&#x5B89;&#x88C5;</p>
<pre><code class="lang-shell">pip install djangorestframework-jwt
</code></pre>
<p>&#x914D;&#x7F6E;</p>
<pre><code class="lang-python">REST_FRAMEWORK = {
    <span class="hljs-string">&apos;DEFAULT_AUTHENTICATION_CLASSES&apos;</span>: (
        <span class="hljs-string">&apos;rest_framework_jwt.authentication.JSONWebTokenAuthentication&apos;</span>,
        <span class="hljs-string">&apos;rest_framework.authentication.SessionAuthentication&apos;</span>,
        <span class="hljs-string">&apos;rest_framework.authentication.BasicAuthentication&apos;</span>,
    ),
}

JWT_AUTH = {
    <span class="hljs-string">&apos;JWT_EXPIRATION_DELTA&apos;</span>: datetime.timedelta(days=<span class="hljs-number">1</span>),
}
</code></pre>
<ul>
<li>JWT_EXPIRATION_DELTA  &#x6307;&#x660E;token&#x7684;&#x6709;&#x6548;&#x671F;</li>
</ul>
<h1 id="&#x8D26;&#x53F7;&#x767B;&#x5F55;">&#x8D26;&#x53F7;&#x767B;&#x5F55;</h1>
<h3 id="1--&#x4E1A;&#x52A1;&#x8BF4;&#x660E;">1.  &#x4E1A;&#x52A1;&#x8BF4;&#x660E;</h3>
<p>&#x9A8C;&#x8BC1;&#x7528;&#x6237;&#x540D;&#x548C;&#x5BC6;&#x7801;&#xFF0C;&#x9A8C;&#x8BC1;&#x6210;&#x529F;&#x540E;&#xFF0C;&#x4E3A;&#x7528;&#x6237;&#x7B7E;&#x53D1;JWT&#xFF0C;&#x524D;&#x7AEF;&#x5C06;&#x7B7E;&#x53D1;&#x7684;JWT&#x4FDD;&#x5B58;&#x4E0B;&#x6765;&#x3002;</p>
<h3 id="2--&#x540E;&#x7AEF;&#x63A5;&#x53E3;&#x8BBE;&#x8BA1;">2.  &#x540E;&#x7AEF;&#x63A5;&#x53E3;&#x8BBE;&#x8BA1;</h3>
<p><strong>&#x8BF7;&#x6C42;&#x65B9;&#x5F0F;</strong>&#xFF1A; POST   <code>meiduo_admin/authorizations/</code></p>
<p><strong>&#x8BF7;&#x6C42;&#x53C2;&#x6570;</strong>&#xFF1A;  JSON &#x6216; &#x8868;&#x5355;</p>
<table>
<thead>
<tr>
<th>&#x53C2;&#x6570;&#x540D;</th>
<th>&#x7C7B;&#x578B;</th>
<th>&#x662F;&#x5426;&#x5FC5;&#x987B;</th>
<th>&#x8BF4;&#x660E;</th>
</tr>
</thead>
<tbody>
<tr>
<td>username</td>
<td>str</td>
<td>&#x662F;</td>
<td>&#x7528;&#x6237;&#x540D;</td>
</tr>
<tr>
<td>password</td>
<td>str</td>
<td>&#x662F;</td>
<td>&#x5BC6;&#x7801;</td>
</tr>
</tbody>
</table>
<p><strong>&#x8FD4;&#x56DE;&#x6570;&#x636E;</strong>&#xFF1A;  JSON</p>
<pre><code class="lang-json">{
    <span class="hljs-string">&quot;username&quot;</span>: <span class="hljs-string">&quot;python&quot;</span>,
    <span class="hljs-string">&quot;user_id&quot;</span>: <span class="hljs-number">1</span>,
    <span class="hljs-string">&quot;token&quot;</span>: <span class="hljs-string">&quot;eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjo5LCJ1c2VybmFtZSI6InB5dGhvbjgiLCJleHAiOjE1MjgxODI2MzQsImVtYWlsIjoiIn0.ejjVvEWxrBvbp18QIjQbL1TFE0c0ejQgizui_AROlAU&quot;</span>
}
</code></pre>
<table>
<thead>
<tr>
<th>&#x8FD4;&#x56DE;&#x503C;</th>
<th>&#x7C7B;&#x578B;</th>
<th>&#x662F;&#x5426;&#x5FC5;&#x987B;</th>
<th>&#x8BF4;&#x660E;</th>
</tr>
</thead>
<tbody>
<tr>
<td>username</td>
<td>str</td>
<td>&#x662F;</td>
<td>&#x7528;&#x6237;&#x540D;</td>
</tr>
<tr>
<td>id</td>
<td>int</td>
<td>&#x662F;</td>
<td>&#x7528;&#x6237;id</td>
</tr>
<tr>
<td>token</td>
<td>str</td>
<td>&#x662F;</td>
<td>&#x8EAB;&#x4EFD;&#x8BA4;&#x8BC1;&#x51ED;&#x636E;</td>
</tr>
</tbody>
</table>
<h3 id="3-&#x540E;&#x7AEF;&#x5B9E;&#x73B0;">3. &#x540E;&#x7AEF;&#x5B9E;&#x73B0;</h3>
<p>Django REST framework JWT&#x63D0;&#x4F9B;&#x4E86;&#x767B;&#x5F55;&#x7B7E;&#x53D1;JWT&#x7684;&#x89C6;&#x56FE;&#xFF0C;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x4F7F;&#x7528;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> rest_framework_jwt.views <span class="hljs-keyword">import</span> obtain_jwt_token

urlpatterns = [
    url(<span class="hljs-string">r&apos;^authorizations/$&apos;</span>, obtain_jwt_token),
]
</code></pre>
<p>&#x4F46;&#x662F;&#x9ED8;&#x8BA4;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x4EC5;&#x6709;token&#xFF0C;&#x6211;&#x4EEC;&#x8FD8;&#x9700;&#x5728;&#x8FD4;&#x56DE;&#x503C;&#x4E2D;&#x589E;&#x52A0;username&#x548C;user_id&#x3002;</p>
<p>&#x901A;&#x8FC7;&#x4FEE;&#x6539;&#x8BE5;&#x89C6;&#x56FE;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x53EF;&#x4EE5;&#x5B8C;&#x6210;&#x6211;&#x4EEC;&#x7684;&#x9700;&#x6C42;&#x3002;</p>
<p>&#x5728;users/utils.py &#x4E2D;&#xFF0C;&#x521B;&#x5EFA;</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">jwt_response_payload_handler</span><span class="hljs-params">(token, user=None, request=None)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;
    &#x81EA;&#x5B9A;&#x4E49;jwt&#x8BA4;&#x8BC1;&#x6210;&#x529F;&#x8FD4;&#x56DE;&#x6570;&#x636E;
    &quot;&quot;&quot;</span>
    <span class="hljs-keyword">return</span> {
        <span class="hljs-string">&apos;token&apos;</span>: token,
        <span class="hljs-string">&apos;id&apos;</span>: user.id,
        <span class="hljs-string">&apos;username&apos;</span>: user.username
    }
</code></pre>
<p>&#x4FEE;&#x6539;&#x914D;&#x7F6E;&#x6587;&#x4EF6;</p>
<pre><code class="lang-python"><span class="hljs-comment"># JWT&#x914D;&#x7F6E;</span>
JWT_AUTH = {
    <span class="hljs-string">&apos;JWT_EXPIRATION_DELTA&apos;</span>: datetime.timedelta(days=<span class="hljs-number">1</span>),
    <span class="hljs-string">&apos;JWT_RESPONSE_PAYLOAD_HANDLER&apos;</span>: <span class="hljs-string">&apos;meiduo_admin.utils.jwt_response.jwt_response_payload_handler&apos;</span>,
}
</code></pre>
<h3 id="4--&#x589E;&#x52A0;&#x652F;&#x6301;&#x7BA1;&#x7406;&#x5458;&#x7528;&#x6237;&#x767B;&#x5F55;&#x8D26;&#x53F7;">4.  &#x589E;&#x52A0;&#x652F;&#x6301;&#x7BA1;&#x7406;&#x5458;&#x7528;&#x6237;&#x767B;&#x5F55;&#x8D26;&#x53F7;</h3>
<p>JWT&#x6269;&#x5C55;&#x7684;&#x767B;&#x5F55;&#x89C6;&#x56FE;&#xFF0C;&#x5728;&#x6536;&#x5230;&#x7528;&#x6237;&#x540D;&#x4E0E;&#x5BC6;&#x7801;&#x65F6;&#xFF0C;&#x4E5F;&#x662F;&#x8C03;&#x7528;Django&#x7684;&#x8BA4;&#x8BC1;&#x7CFB;&#x7EDF;&#x4E2D;&#x63D0;&#x4F9B;&#x7684;<strong>authenticate()</strong>&#x6765;&#x68C0;&#x67E5;&#x7528;&#x6237;&#x540D;&#x4E0E;&#x5BC6;&#x7801;&#x662F;&#x5426;&#x6B63;&#x786E;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x4FEE;&#x6539;Django&#x8BA4;&#x8BC1;&#x7CFB;&#x7EDF;&#x7684;&#x8BA4;&#x8BC1;&#x540E;&#x7AEF;&#xFF08;&#x4E3B;&#x8981;&#x662F;authenticate&#x65B9;&#x6CD5;&#xFF09;&#x6765;&#x652F;&#x6301;&#x767B;&#x5F55;&#x8D26;&#x53F7;&#x65E2;&#x53EF;&#x4EE5;&#x662F;&#x7528;&#x6237;&#x540D;&#x4E5F;&#x53EF;&#x4EE5;&#x662F;&#x624B;&#x673A;&#x53F7;&#x3002;</p>
<p><strong>&#x4FEE;&#x6539;Django&#x8BA4;&#x8BC1;&#x7CFB;&#x7EDF;&#x7684;&#x8BA4;&#x8BC1;&#x540E;&#x7AEF;&#x9700;&#x8981;&#x7EE7;&#x627F;<code>django.contrib.auth.backends.ModelBackend</code>&#xFF0C;&#x5E76;&#x91CD;&#x5199;authenticate&#x65B9;&#x6CD5;&#x3002;</strong></p>
<p><code>authenticate(self, request, username=None, password=None, **kwargs)</code>&#x65B9;&#x6CD5;&#x7684;&#x53C2;&#x6570;&#x8BF4;&#x660E;&#xFF1A;</p>
<ul>
<li>request  &#x672C;&#x6B21;&#x8BA4;&#x8BC1;&#x7684;&#x8BF7;&#x6C42;&#x5BF9;&#x8C61;</li>
<li>username  &#x672C;&#x6B21;&#x8BA4;&#x8BC1;&#x63D0;&#x4F9B;&#x7684;&#x7528;&#x6237;&#x8D26;&#x53F7;</li>
<li>password  &#x672C;&#x6B21;&#x8BA4;&#x8BC1;&#x63D0;&#x4F9B;&#x7684;&#x5BC6;&#x7801;</li>
</ul>
<p><strong>&#x6211;&#x4EEC;&#x60F3;&#x8981;&#x8BA9;&#x7BA1;&#x7406;&#x5458;&#x7528;&#x6237;&#x624D;&#x80FD;&#x767B;&#x5F55;&#x6211;&#x4EEC;&#x7684;admin&#x540E;&#x53F0;,&#x8FD9;&#x65F6;&#x6211;&#x4EEC;&#x5C31;&#x8981;&#x4FEE;&#x6539;django&#x539F;&#x6709;&#x7684;&#x7528;&#x6237;&#x9A8C;&#x8BC1;&#x65B9;&#x6CD5;&#x3002;</strong></p>
<p>&#x91CD;&#x5199;authenticate&#x65B9;&#x6CD5;&#x7684;&#x601D;&#x8DEF;&#xFF1A;</p>
<ol>
<li>&#x6839;&#x636E;username&#x53C2;&#x6570;&#x67E5;&#x627E;&#x7528;&#x6237;User&#x5BF9;&#x8C61;&#xFF0C;&#x5728;&#x67E5;&#x8BE2;&#x6761;&#x4EF6;&#x4E2D;&#x5728;&#x52A0;&#x4E0A;is_staff=True&#x7684;&#x6761;&#x4EF6;</li>
<li>&#x82E5;&#x67E5;&#x627E;&#x5230;User&#x5BF9;&#x8C61;&#xFF0C;&#x8C03;&#x7528;User&#x5BF9;&#x8C61;&#x7684;check_password&#x65B9;&#x6CD5;&#x68C0;&#x67E5;&#x5BC6;&#x7801;&#x662F;&#x5426;&#x6B63;&#x786E;</li>
</ol>
<p>&#x5728;meiduo_mall/utils/authenticate.py&#x4E2D;&#x7F16;&#x5199;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django.contrib.auth.backends <span class="hljs-keyword">import</span> ModelBackend
<span class="hljs-keyword">import</span> re
<span class="hljs-keyword">from</span> users.models <span class="hljs-keyword">import</span> User


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MeiduoModelBackend</span><span class="hljs-params">(ModelBackend)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">authenticate</span><span class="hljs-params">(self, request, username=None, password=None, **kwargs)</span>:</span>
        <span class="hljs-comment"># &#x5224;&#x65AD;&#x662F;&#x5426;&#x901A;&#x8FC7;vue&#x7EC4;&#x4EF6;&#x53D1;&#x9001;&#x8BF7;&#x6C42;</span>
        <span class="hljs-keyword">if</span> request <span class="hljs-keyword">is</span> <span class="hljs-keyword">None</span>:
            <span class="hljs-keyword">try</span>:
                user = User.objects.get(username=username, is_staff=<span class="hljs-keyword">True</span>)
            <span class="hljs-keyword">except</span>:
                <span class="hljs-keyword">return</span> <span class="hljs-keyword">None</span>
            <span class="hljs-comment"># &#x5224;&#x65AD;&#x5BC6;&#x7801;</span>
            <span class="hljs-keyword">if</span> user.check_password(password):
                <span class="hljs-keyword">return</span> user

        <span class="hljs-keyword">else</span>:
            <span class="hljs-comment"># &#x53D8;&#x91CF;username&#x7684;&#x503C;&#xFF0C;&#x53EF;&#x4EE5;&#x662F;&#x7528;&#x6237;&#x540D;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x662F;&#x624B;&#x673A;&#x53F7;&#xFF0C;&#x9700;&#x8981;&#x5224;&#x65AD;&#xFF0C;&#x518D;&#x67E5;&#x8BE2;</span>
            <span class="hljs-keyword">try</span>:
                <span class="hljs-comment"># if re.match(r&apos;^1[3-9]\d{9}$&apos;, username):</span>
                <span class="hljs-comment">#     user = User.objects.get(mobile=username)</span>
                <span class="hljs-comment"># else:</span>
                <span class="hljs-comment">#     user = User.objects.get(username=username)</span>
                user = User.objects.get(username=username)
            <span class="hljs-keyword">except</span>:
                <span class="hljs-comment"># &#x5982;&#x679C;&#x672A;&#x67E5;&#x5230;&#x6570;&#x636E;&#xFF0C;&#x5219;&#x8FD4;&#x56DE;None&#xFF0C;&#x7528;&#x4E8E;&#x540E;&#x7EED;&#x5224;&#x65AD;</span>
                <span class="hljs-keyword">try</span>:
                    user = User.objects.get(mobile=username)
                <span class="hljs-keyword">except</span>:
                    <span class="hljs-keyword">return</span> <span class="hljs-keyword">None</span>
                    <span class="hljs-comment"># return None</span>

            <span class="hljs-comment"># &#x5224;&#x65AD;&#x5BC6;&#x7801;</span>
            <span class="hljs-keyword">if</span> user.check_password(password):
                <span class="hljs-keyword">return</span> user
            <span class="hljs-keyword">else</span>:
                <span class="hljs-keyword">return</span> <span class="hljs-keyword">None</span>
</code></pre>
<p>&#x5728;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x4E2D;&#x544A;&#x77E5;Django&#x4F7F;&#x7528;&#x6211;&#x4EEC;&#x81EA;&#x5B9A;&#x4E49;&#x7684;&#x8BA4;&#x8BC1;&#x540E;&#x7AEF;</p>
<h4 id="&#x524D;&#x7AEF;&#x4FDD;&#x5B58;token">&#x524D;&#x7AEF;&#x4FDD;&#x5B58;token</h4>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5C06;JWT&#x4FDD;&#x5B58;&#x5728;cookie&#x4E2D;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x4FDD;&#x5B58;&#x5728;&#x6D4F;&#x89C8;&#x5668;&#x7684;&#x672C;&#x5730;&#x5B58;&#x50A8;&#x91CC;&#xFF0C;&#x6211;&#x4EEC;&#x4FDD;&#x5B58;&#x5728;&#x6D4F;&#x89C8;&#x5668;&#x672C;&#x5730;&#x5B58;&#x50A8;&#x4E2D;</p>
<p>&#x6D4F;&#x89C8;&#x5668;&#x7684;&#x672C;&#x5730;&#x5B58;&#x50A8;&#x63D0;&#x4F9B;&#x4E86;sessionStorage &#x548C; localStorage &#x4E24;&#x79CD;&#xFF1A;</p>
<ul>
<li><strong>sessionStorage</strong>  &#x6D4F;&#x89C8;&#x5668;&#x5173;&#x95ED;&#x5373;&#x5931;&#x6548;</li>
<li><strong>localStorage</strong>  &#x957F;&#x671F;&#x6709;&#x6548;</li>
</ul>
<p>&#x4F7F;&#x7528;&#x65B9;&#x6CD5;</p>
<pre><code class="lang-js">sessionStorage.&#x53D8;&#x91CF;&#x540D; = &#x53D8;&#x91CF;&#x503C;   <span class="hljs-comment">// &#x4FDD;&#x5B58;&#x6570;&#x636E;</span>
sessionStorage.&#x53D8;&#x91CF;&#x540D;  <span class="hljs-comment">// &#x8BFB;&#x53D6;&#x6570;&#x636E;</span>
sessionStorage.clear()  <span class="hljs-comment">// &#x6E05;&#x9664;&#x6240;&#x6709;sessionStorage&#x4FDD;&#x5B58;&#x7684;&#x6570;&#x636E;</span>

localStorage.&#x53D8;&#x91CF;&#x540D; = &#x53D8;&#x91CF;&#x503C;   <span class="hljs-comment">// &#x4FDD;&#x5B58;&#x6570;&#x636E;</span>
localStorage.&#x53D8;&#x91CF;&#x540D;  <span class="hljs-comment">// &#x8BFB;&#x53D6;&#x6570;&#x636E;</span>
localStorage.clear()  <span class="hljs-comment">// &#x6E05;&#x9664;&#x6240;&#x6709;localStorage&#x4FDD;&#x5B58;&#x7684;&#x6570;&#x636E;</span>
</code></pre>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> vm = <span class="hljs-keyword">new</span> Vue({
    ...
    methods: {
        ...
        on_submit: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
            axios.post(...)
                .then(response =&gt; {
                    <span class="hljs-comment">// &#x8BB0;&#x5F55;&#x7528;&#x6237;&#x7684;&#x767B;&#x5F55;&#x72B6;&#x6001;</span>
                    sessionStorage.clear();
                    localStorage.clear();
                    localStorage.token = response.data.token;
                    localStorage.username = response.data.username;
                    localStorage.user_id = response.data.id;
                    location.href = <span class="hljs-string">&apos;/index.html&apos;</span>;
                })
                .catch(...)
        }
    }
})
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../../data/logins/jwt.html" class="navigation navigation-prev " aria-label="Previous page: JWT的原理和构成"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../../data/statistics/index.html" class="navigation navigation-next " aria-label="Next page: 数据统计"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../../gitbook/app.js"></script>

    
    <script src="../../gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-livereload/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"disqus":{"shortName":"gitbookuse"},"github":{"url":"https://github.com/dododream"},"search-pro":{"cutWordLib":"nodejieba","defineWord":["gitbook-use"]},"sharing":{"weibo":true,"facebook":true,"twitter":true,"google":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"tbfed-pagefooter":{"copyright":"Copyright © ITCast","modify_label":"「Revision Time:","modify_format":"YYYY-MM-DD HH:mm:ss」"},"baidu":{"token":"ff100361cdce95dd4c8fb96b4009f7bc"},"sitemap":{"hostname":"http://www.treenewbee.top"},"donate":{"wechat":"http://weixin.png","alipay":"http://alipay.png","title":"","button":"赏","alipayText":"支付宝打赏","wechatText":"微信打赏"},"edit-link":{"base":"https://github.com/dododream/edit","label":"Edit This Page"},"splitter":{},"toggle-chapters":{},"highlight":{},"fontsettings":{"theme":"white","family":"sans","size":2},"livereload":{}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
    </body>
    <!-- End of book Django课程讲义 -->
</html>
